layui.use(['form','treeSelect','layer','upload','laydate','layedit','tableSelect'],function(){
    var form = layui.form
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;
    var upload = layui.upload;
    var laydate = layui.laydate;
    var treeSelect= layui.treeSelect;//树形下拉框
    var imgURL="";//多图片上传的路径





    var produceDesc;


    //回显数据
    var request = GetRequest();
    $.get("/admin/product/getProduct?productId=" + request.id,function (data) {
        $("#produceId").val(data.produceId);
        $("#produceName").val(data.produceName);
        $("#supplier").val(data.supplier);
        $("#produceNo").val(data.produceNo);
        var date = new Date(data.supplyDate);
        laydate.render({
            elem: '#supplyDate'
            ,value: date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()   //必须遵循format参数设定的格式  + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds()
        });
        $("#buyer").val(data.buyer);
        $("#buyNum").val(data.buyNum);
        $("#unit").val(data.unit);
        $("#price").val(data.price);
        $("#threshold").val(data.threshold);
        $("#produceBrief").val(data.produceBrief);
        $("#remarks").text(data.remarks);
        $.ajax({
            //根据parentId查对应类别，从而拿到父类结点的名称
            url: '/admin/productCategory/getProductCategory?ProductCategoryId='+data.categoryId,
            type: 'GET',
            success: function (da) {
                if(da!=null) {
                        //渲染菜单的数据到表单元素
                        treeSelect.render({
                            elem: '#categoryName',
                            data: '/admin/productCategory/treelist',
                            type: 'get',
                            placeholder: da.name,// 占位符
                            search: true,// 是否开启搜索功能：true/false，默认false
                            click: function(d){
                                $("#categoryName").val(d.current.name);
                                $("#categoryId").val(d.current.id);
                            },
                            success: function () { // 加载完成后的回调函数
                                $("#categoryName").val(da.name);
                                $("#categoryId").val(da.id);
                            }
                        });
                }
            }})

        $.ajax({
            //根据parentId查对应类别，从而拿到父类结点的名称
            url: '/admin/storage/getStorage?StorageId='+data.storageId,
            type: 'GET',
            success: function (da) {
                if(da!=null) {
                    //渲染菜单的数据到表单元素
                    treeSelect.render({
                        elem: '#storageName',
                        data: '/admin/storage/treelist',
                        type: 'get',
                        placeholder: da.name,// 占位符
                        search: true,// 是否开启搜索功能：true/false，默认false
                        click: function(d){
                            $("#storageId").val(d.current.storageId);
                            $("#storageName").val(d.current.name);
                        },
                        success: function () { // 加载完成后的回调函数
                            $("#storageName").val(da.name);
                            $("#storageId").val(da.storageId);
                        }
                    });

                }
            }})
    });



    form.on("submit(edit)",function(data){
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        $.post("/admin/product/edit",{
            categoryId:$("#categoryId").val(),
            produceId: $("#produceId").val(),
            produceName: $("#produceName").val(),
            supplier: $("#supplier").val(),
            produceNo : $("#produceNo").val(),
            buyer: $("#buyer").val(),
            buyNum : $("#buyNum").val(),
            unit: $("#unit").val(),
            price : $("#price").val(),
            supplyDate : new Date(Date.parse($("#supplyDate").val())),
            storageId : $("#storageId").val(),
            threshold : $("#threshold").val(),
            produceBrief : $("#produceBrief").val(),
            remarks:$("#remarks").val()
        },function(res){

        })
        setTimeout(function(){
            top.layer.close(index);
            top.layer.msg("记录编辑成功！");
            layer.closeAll("iframe");
            //刷新父页面
            parent.location.reload();
        },2000);
        return false;
    })


    //获取路径中的参数
    function GetRequest() {
        var url = location.search;         //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }


});